﻿using System;
using System.Windows;
using System.Windows.Controls;
using GameEditor.View.Components;
using GameEngine.View.Components;
using MMOG;
using MapEngine;

namespace SilverlightClientEditor.View.Components
{
    public partial class MapPropertiesEditorUC : UserControl, IMapEditorMenuView
    {
        public Point MapSize {
            get { return new Point(int.Parse(WidthTextBox.Text),int.Parse(HeightTextBox.Text)); }
            set { 
                WidthTextBox.Text = value.X.ToString();
                HeightTextBox.Text = value.Y.ToString();
            }
        }
        public TileType TileType
        {
            get {
                return (TileType)Enum.Parse(typeof(TileType), TileTypeComboBox.SelectedItem as string,false);
            }
            set
            {
                TileTypeComboBox.SelectedItem = value.ToString();
            }
        }

        public MapType MapType
        {
            get
            {
                return (MapType)Enum.Parse(typeof(MapType), MapTypeComboBox.SelectedItem as string, false);
            }
            set
            {
                MapTypeComboBox.SelectedItem = value.ToString();
            }
        }

        public float PerlinFrequency
        {
            get { return float.Parse(FrequencyTextBox.Text); }
            set { FrequencyTextBox.Text = value.ToString(); }
        }
        public Point PerlinOrigin
        {
            get { return new Point(float.Parse(OriginXTextBox.Text),float.Parse(OriginYTextBox.Text)); }
            set { 
                OriginXTextBox.Text = value.X.ToString();
                OriginYTextBox.Text = value.Y.ToString(); 
            }
        }
        public float PerlinAmplitude
        {
            get { return float.Parse(AmplitudeTextBox.Text); }
            set { AmplitudeTextBox.Text = value.ToString(); }
        }
        public float PerlinPersistence
        {
            get { return float.Parse(PersistenceTextBox.Text); }
            set { PersistenceTextBox.Text = value.ToString(); }
        }
        public int PerlinOctaves
        {
            get { return int.Parse(OctavesTextBox.Text); }
            set { OctavesTextBox.Text = value.ToString(); }
        }
        public int PerlinSeed
        {
            get { return int.Parse(SeedTextBox.Text); }
            set { SeedTextBox.Text = value.ToString(); }
        }

        public bool TerrainDownButtonPressed
        {
            get;
            protected set;
        }
        public bool TerrainUpButtonPressed
        {
            get;
            protected set;
        }

        //public event OnButtonClickHandler OnButtonClick;

        public MapPropertiesEditorUC()
        {
            InitializeComponent();
            foreach (string type in SilverlightHelper.GetEnumStrings <TileType>())
                TileTypeComboBox.Items.Add(type);
            foreach (string type in SilverlightHelper.GetEnumStrings<MapType>())
                MapTypeComboBox.Items.Add(type);
            TerrainUpButtonPressed = false;
            TerrainDownButtonPressed = false;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string buttonName = ((Button)sender).Name;
            if(sender.Equals(UpButton))
            {
                TerrainUpButtonPressed = !TerrainUpButtonPressed;
                if (TerrainUpButtonPressed)
                {
                    DownButton.IsEnabled = false;
                    GenerateButton.IsEnabled = false;
                    SetButton.IsEnabled = false;
                }
                else {
                    DownButton.IsEnabled = true;
                    UpButton.IsEnabled = true;
                    GenerateButton.IsEnabled = true;
                    SetButton.IsEnabled = true;
                }
                if (OnTerrainUp != null)
                    OnTerrainUp(null);
            }
            else if(sender.Equals(DownButton))
            {
                
                TerrainDownButtonPressed = !TerrainDownButtonPressed;
                if (TerrainUpButtonPressed)
                {
                    UpButton.IsEnabled = false;
                    GenerateButton.IsEnabled = false;
                    SetButton.IsEnabled = false;
                }
                else
                {
                    DownButton.IsEnabled = true;
                    UpButton.IsEnabled = true;
                    GenerateButton.IsEnabled = true;
                    SetButton.IsEnabled = true;
                } 
                if (OnTerrainDown != null)
                    OnTerrainDown(null);
            }
            else if(sender.Equals(GenerateButton))
            {
                if (OnMapGenerateHeights != null)
                    OnMapGenerateHeights(null);
            }
            else if (sender.Equals(SetButton))
            {
                if (OnMapPropertiesSet != null)
                    OnMapPropertiesSet(null);
            }  
        }

        #region ITileMapEditorMenuView Members

        public event Action<Point> OnSizeChanged;
        public event Action<object> OnMapPropertiesSet;
        public event Action<object> OnMapGenerateHeights;
        public event Action<object> OnTerrainUp;
        public event Action<object> OnTerrainDown;

        #endregion
    }
}